import cn.zhiwen.ConsumerApplication;
import cn.zhiwen.config.HttpEntityEnclosingDeleteRequest;
import cn.zhiwen.consumerController.ConsumerController;
import com.google.gson.Gson;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest(classes = ConsumerApplication.class)
@WebAppConfiguration
public class RegistDemo {

    private MockMvc mvc;

    @Before
    public void setUp() {
        mvc = MockMvcBuilders.standaloneSetup(new ConsumerController()).build();
    }

    @Test
    public void hello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)).
                andExpect(status().isOk()).andExpect(content().string(equalTo("Hello World")));
    }

    @Test
    public void registerUser() throws Exception {
        mvc.perform(MockMvcRequestBuilders.post("/registerUser").accept(MediaType.APPLICATION_JSON));
    }

    @Test
    public void deleteSetBody() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {
            @Override
            protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
                if (HttpMethod.DELETE == httpMethod) {
                    return new HttpEntityEnclosingDeleteRequest(uri);
                }
                return super.createHttpUriRequest(httpMethod, uri);
            }
        });

        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        Map<String, String> map = new HashMap<>();
        map.put("body","some sample body sent along the DELETE request");
        Gson gson = new Gson();
        String s = gson.toJson(map);

        ResponseEntity<String> exchange = restTemplate.exchange(
                "http://localhost:8004/springConsumer01/deleteUser",
                HttpMethod.DELETE,
                new HttpEntity<String>(s,headers),
                String.class);
        String body = exchange.getBody();
        System.out.println(body);
    }

}


